package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ZxingQrcodeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.IotQrcode;
import com.ruoyi.system.service.IIotQrcodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 二维码Controller
 * 
 * @author ruoyi
 * @date 2021-05-18
 */
@RestController
@RequestMapping("/system/qrcode")
public class IotQrcodeController extends BaseController
{
    @Autowired
    private IIotQrcodeService iotQrcodeService;

    /**
     * 查询二维码列表
     */
    @PreAuthorize("@ss.hasPermi('system:qrcode:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotQrcode iotQrcode)
    {
        startPage();
        List<IotQrcode> list = iotQrcodeService.selectIotQrcodeList(iotQrcode);
        return getDataTable(list);
    }

    /**
     * 导出二维码列表
     */
    @PreAuthorize("@ss.hasPermi('system:qrcode:export')")
    @Log(title = "二维码", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(IotQrcode iotQrcode)
    {
        List<IotQrcode> list = iotQrcodeService.selectIotQrcodeList(iotQrcode);
        ExcelUtil<IotQrcode> util = new ExcelUtil<IotQrcode>(IotQrcode.class);
        return util.exportExcel(list, "二维码数据");
    }

    /**
     * 获取二维码详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:qrcode:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(iotQrcodeService.selectIotQrcodeById(id));
    }

    /**
     * 新增二维码
     */
    @PreAuthorize("@ss.hasPermi('system:qrcode:add')")
    @Log(title = "二维码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotQrcode iotQrcode)
    {
        return toAjax(iotQrcodeService.insertIotQrcode(iotQrcode));
    }

    /**
     * 修改二维码
     */
    @PreAuthorize("@ss.hasPermi('system:qrcode:edit')")
    @Log(title = "二维码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotQrcode iotQrcode)
    {
        return toAjax(iotQrcodeService.updateIotQrcode(iotQrcode));
    }

    /**
     * 删除二维码
     */
    @PreAuthorize("@ss.hasPermi('system:qrcode:remove')")
    @Log(title = "二维码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(iotQrcodeService.deleteIotQrcodeByIds(ids));
    }


    @GetMapping("/img/{id}")
    public void qrcodeImg(@PathVariable String id,HttpServletResponse response){
        ZxingQrcodeUtils.encodeQrcode("id", response);
    }



}
